#!/bin/sh

#chkconfig: 2345 99 99
### BEGIN INIT INFO
# Provides:          cns
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start cns daemon at boot time
# Description:       Start cns daemon at boot time
### END INIT INFO
START=99
EXTRA_COMMANDS="status"

usage()
{
	if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
		us="systemctl [start|stop|status|restart] cns.service"
	else
		if [ "$0" = '/etc/rc.common' ]; then
			us="/etc/init.d/cns [start|stop|status|restart]"\
		else
			us="$0 [start|stop|status|restart]"
		fi
	fi &>/dev/null
	echo "Usage:
	$us
	Config file is [cns_install_dir]/cns.json"
}

status()
{
	{
		grep -q cns /proc/`cat "[cns_install_dir]/run.pid" 2>/dev/null`/comm 2>/dev/null && \
			echo "cns is running..." || \
			echo "cns is stopped..."
	} 2>/dev/null
}


start()
{
	status | grep running && return 0
	echo -n "Starting cns:"
	for server_port in [cns_tcp_port_list]; do
		iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
		ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
	done 2>/dev/null
	cd "[cns_install_dir]"
	rm -f "[cns_install_dir]/run.pid"
	./cns -json=cns.json -daemon >/dev/null
	count=0
	while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
		sleep 1
		count=$((count + 1))
	done
	grep -q cns /proc/`cat [cns_install_dir]/run.pid 2>/dev/null`/comm && \
		echo -e "\033[60G[\033[32m  OK  \033[0m]" || \
		echo -e "\033[60G[\033[31mFAILED\033[0m]"
}

stop()
{
	for server_port in [cns_tcp_port_list]; do
		while iptables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done
		while ip6tables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done
	done 2>/dev/null
	status | grep stopped && return 0
	echo -n "Stopping cns:"
	kill `cat [cns_install_dir]/run.pid 2>/dev/null` 2>/dev/null
	kill -9 `pidof cns` >/dev/null 2>&1
    sleep 1
	grep -q cns /proc/`cat [cns_install_dir]/run.pid`/comm 2>/dev/null && \
		echo -e "\033[60G[\033[31mFAILED\033[0m]" || \
		echo -e "\033[60G[\033[32m  OK  \033[0m]"
}

restart()
{
	stop
	start
}

systemdStart()
{
	status | grep running && return 0
	cd "[cns_install_dir]"
	rm -f "[cns_install_dir]/run.pid"
	for server_port in [cns_tcp_port_list]; do
		iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
		ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
	done 2>/dev/null
	./cns -json=cns.json >/dev/null
	count=0
	while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
		sleep 1
		count=$((count + 1))
	done
}

systemdRestart()
{
	stop
	systemdStart
}

if [ -z "$initscript" ]; then
	${1:-usage}
fi
